<?php

namespace Modules\ShopAdmin\Models;
use Modules\AuthAdmin\Services\CommonService;
class ShopProductType extends BaseApiModel
{
	/** 
     * @name  关联图片
     */
    public function image_one()
    {
        return $this->hasOne('Modules\AuthAdmin\Models\AuthImage','id','image_id');
    }
	/**
     * @name   显示状态:0=隐藏,1=显示,0和1修改为true或false
     * @param  int  $value
     * @return Boolean 
     */
    public function getStatusAttribute($value)
    {
        return $value==1?true:false;
    }
	/**
     * @name   更新时间为null时返回
     * @param  int  $value
     * @return Boolean 
     */
    public function getUpdatedAtAttribute($value)
    {
        return $value?$value:'';
    }
	/**
	 * @name  商品分类列表数据
	 * @return json 
	 */
    public function product_type_data(){
        $list = $this->with([
					'image_one'=>function($query){
						$query->select('id','url','open');  
					}
				])->orderBy('sort','asc') 
				->orderBy('id','desc') 
				->get()
				->toArray();
		return $this->api_success(['data'=>$this->product_type_arr($list)]);		
	}
	/**
	 * @name  获取商品分类数据
	 * @return json 
	 */
    public function product_type_list(){
        $list = $this->select('id','pid','title')->orderBy('sort','asc') 
				->orderBy('id','desc') 
				->get()
				->toArray();
		return $this->api_success(['data'=>$this->product_type_arr($list)]);		
	}
	
	/**
	 * @name  商品分类处理数据
	 * @param  product_type_arr  array 商品分类列表数据
	 * @return json 
	 */
	public function product_type_arr($product_type_arr){
		$rule_arr = [];
		$CommonService = new CommonService();
		$i = 0;
		foreach($product_type_arr as $k=>$v){
			if($v['pid'] == 0){
				if(isset($v['image_one']) && $v['image_one']){
					if($v['image_one']['open'] == 1){
					$v['image_url'] = $CommonService->get_http_type().$v['image_one']['url'];
					}else{
						$v['image_url'] = $v['image_one']['url'];
					}
					unset($v['image_one']);
				}
				$rule_arr[$i] = $v;
				unset($product_type_arr[$k]);
				$j = 0;
				foreach($product_type_arr as $k1=>$v1){
					if($rule_arr[$i]['id'] == $v1['pid']){
						if(isset($v1['image_one']) && $v1['image_one']){
							if($v1['image_one']['open'] == 1){
							$v1['image_url'] = $CommonService->get_http_type().$v1['image_one']['url'];
							}else{
								$v1['image_url'] = $v1['image_one']['url'];
							}
							unset($v1['image_one']);
						}
						$rule_arr[$i]['children'][$j] = $v1;
						unset($product_type_arr[$k1]);
						$s = 0;
						foreach($product_type_arr as $k2=>$v2){
							if($rule_arr[$i]['children'][$j]['id'] == $v2['pid']){
								if(isset($v2['image_one']) && $v2['image_one']){
									if($v2['image_one']['open'] == 1){
									$v2['image_url'] = $CommonService->get_http_type().$v2['image_one']['url'];
									}else{
										$v2['image_url'] = $v2['image_one']['url'];
									}
									unset($v2['image_one']);
								}
								$rule_arr[$i]['children'][$j]['children'][$s] = $v2;
								unset($product_type_arr[$k2]);
								$y = 0;
								foreach($product_type_arr as $k3=>$v3){
									if($rule_arr[$i]['children'][$j]['children'][$s]['id'] == $v3['pid']){
										if(isset($v3['image_one']) && $v3['image_one']){
											if($v3['image_one']['open'] == 1){
											$v3['image_url'] = $CommonService->get_http_type().$v3['image_one']['url'];
											}else{
												$v3['image_url'] = $v3['image_one']['url'];
											}
											unset($v3['image_one']);
										}
										$rule_arr[$i]['children'][$j]['children'][$s]['children'][$y] = $v3;
										unset($product_type_arr[$k3]);
										$y++;
									}
								}
								$s++;
							}
							
						}
						$j++;
					}
				}
				$i++;
			}
		}
		return $rule_arr;
	}
	/**
	 * @name  获取父级id
	 * @param id 当前id
	 * @return return array
	 */
	public function id_arr($id){
		$pid = $this->where(['id'=>$id])->value('pid');
		return [$pid,$id];
	}
}
